package com.zhongxi.guoyuan.adapter;


import android.content.Context;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.entity.user.center.CollectionData;
import com.zhongxi.guoyuan.url.Url;
import com.zhongxi.guoyuan.utils.GlideRoundTransform;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created by Administrator on 2016/12/13.
 */

public class CollectionAdapter extends BaseAdapter {

    private Context context;
    private List<CollectionData> data;

    public CollectionAdapter(Context context, List<CollectionData> data) {
        this.context = context;
        this.data = data;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if(convertView == null){
            switch (data.get(0).getFtype()){
                case 1:
                    holder = new ViewHolder();
                    convertView = LayoutInflater.from(context).inflate(R.layout.adapter_meirong, parent, false);
                    holder.ivImg = (ImageView) convertView.findViewById(R.id.ivImg);
                    holder.ivLimit = (ImageView) convertView.findViewById(R.id.ivLimit);
                    holder.tvName = (TextView) convertView.findViewById(R.id.tvText);
                    holder.tvTime = (TextView) convertView.findViewById(R.id.tv_time);
                    holder.tvTruePrice = (TextView) convertView.findViewById(R.id.tvTruePrice);
                    holder.tvCash = (TextView) convertView.findViewById(R.id.tvCash);
                    holder.tvOriginPrice = (TextView) convertView.findViewById(R.id.tvOriginPrice);
                    holder.tvRewardCoin = (TextView) convertView.findViewById(R.id.tvRewardCoin);
                    holder.tvView = (TextView) convertView.findViewById(R.id.tvView);
                    convertView.setTag(holder);
                    break;
                case 2:
                    holder = new ViewHolder();
                    convertView = LayoutInflater.from(context).inflate(R.layout.adapter_reward_coin, parent, false);
                    holder.ivImg = (ImageView) convertView.findViewById(R.id.ivImg);
                    holder.tvName = (TextView) convertView.findViewById(R.id.tvName);
                    holder.tvDesc = (TextView) convertView.findViewById(R.id.tvDesc);
                    holder.tvPrice = (TextView) convertView.findViewById(R.id.tvPrice);
                    holder.tvView = (TextView) convertView.findViewById(R.id.tvView);
                    convertView.setTag(holder);
                    break;
                case 3:
                    holder = new ViewHolder();
                    convertView = LayoutInflater.from(context).inflate(R.layout.item_collection, parent, false);
                    holder.ivImg = (ImageView) convertView.findViewById(R.id.ivImg);
                    holder.tvName = (TextView) convertView.findViewById(R.id.tvName);
                    holder.tvDesc = (TextView) convertView.findViewById(R.id.tvDesc);
                    holder.tvInnerPrice = (TextView) convertView.findViewById(R.id.tvInnerPrice);
                    holder.tvMarketPrice = (TextView) convertView.findViewById(R.id.tvOriginPrice);
                    convertView.setTag(holder);
                    break;
                case 4:
                    holder = new ViewHolder();
                    convertView = LayoutInflater.from(context).inflate(R.layout.adapter_coin, parent, false);
                    holder.tvName = (TextView) convertView.findViewById(R.id.tvName);
                    holder.ivImg = (ImageView) convertView.findViewById(R.id.ivImg);
                    holder.tvDesc = (TextView) convertView.findViewById(R.id.tvDesc);
                    holder.tvView = (TextView) convertView.findViewById(R.id.tvView);
                    holder.rb = (RatingBar) convertView.findViewById(R.id.rb);
                    convertView.setTag(holder);
                    break;
            }
            convertView.setTag(holder);
        }else{
            switch (data.get(0).getFtype()){
                case 1:
                    holder = (ViewHolder) convertView.getTag();
                    break;
                case 2:
                    holder = (ViewHolder) convertView.getTag();
                    break;
                case 3:
                    holder = (ViewHolder) convertView.getTag();
                    break;
                case 4:
                    holder = (ViewHolder) convertView.getTag();
                    break;
            }
        }
        if(null!=data.get(position).getData()) {
            if (null != data.get(position).getData().getThumbnail() && !"".equals(data.get(position).getData().getThumbnail())) {
                Glide.with(context).load(Url.IMG_URL+data.get(position).getData().getThumbnail()).placeholder(R.mipmap.iv_eg).transform(new GlideRoundTransform(context, 20)).into(holder.ivImg);
            }
            if (null != data.get(position).getData().getName() && !"".equals(data.get(position).getData().getName())) {
                holder.tvName.setText(data.get(position).getData().getName());
            } else {
                holder.tvName.setText("");
            }
            switch (data.get(0).getFtype()) {
                case 1:
                    if (null != data.get(position).getData().getInnerprice() && !"".equals(data.get(position).getData().getInnerprice())) {
                        holder.tvTruePrice.setText(data.get(position).getData().getInnerprice());
                    } else {
                        holder.tvTruePrice.setText("");
                    }
                    if (null != data.get(position).getData().getPrice() && !"".equals(data.get(position).getData().getPrice())) {
                        holder.tvCash.setText("美币+" + data.get(position).getData().getPrice() + "元");
                        holder.tvRewardCoin.setText("[权益会员返"+new BigDecimal(data.get(position).getData().getPrice()).multiply(new BigDecimal(data.get(position).getData().getBacksilverratio())).intValue()/100+"银币]");
                    } else {
                        holder.tvCash.setText("*美币+*元");
                        holder.tvRewardCoin.setText("[权益会员返*银币]");
                    }
                    if (null != data.get(position).getData().getMarketprice() && !"".equals(data.get(position).getData().getMarketprice())) {
                        holder.tvOriginPrice.setText("原价：" + data.get(position).getData().getMarketprice() + "元");
                        holder.tvOriginPrice.setPaintFlags(Paint.STRIKE_THRU_TEXT_FLAG);
                    } else {
                        holder.tvOriginPrice.setText("原价：*元");
                        holder.tvOriginPrice.setPaintFlags(Paint.STRIKE_THRU_TEXT_FLAG);
                    }
                    if (null != data.get(position).getData().getServicetime() && !"".equals(data.get(position).getData().getServicetime())) {
                        holder.tvTime.setText(data.get(position).getData().getServicetime() + "分钟");
                    } else {
                        holder.tvTime.setText("*分钟");
                    }
                    if (null != data.get(position).getData().getViewnum() && !"".equals(data.get(position).getData().getViewnum())) {
                        holder.tvView.setText(data.get(position).getData().getViewnum());
                    } else {
                        holder.tvView.setText("");
                    }
                    if ("0".equals(data.get(position).getData().getIslimited())) {
                        holder.ivLimit.setVisibility(View.GONE);
                    } else {
                        holder.ivLimit.setVisibility(View.VISIBLE);
                    }
                    break;
                case 2:
                    if (null != data.get(position).getData().getSubject() && !"".equals(data.get(position).getData().getSubject())) {
                        holder.tvDesc.setText(data.get(position).getData().getSubject());
                    } else {
                        holder.tvDesc.setText("");
                    }
                    if (null != data.get(position).getData().getPrice() && !"".equals(data.get(position).getData().getPrice())) {
                        holder.tvPrice.setText(data.get(position).getData().getPrice()+"元[全返银币]");
                    } else {
                        holder.tvPrice.setText("");
                    }
                    if (null != data.get(position).getData().getViewnum() && !"".equals(data.get(position).getData().getViewnum())) {
                        holder.tvView.setText(data.get(position).getData().getViewnum());
                    } else {
                        holder.tvView.setText("");
                    }
                    break;
                case 3:
                    if (null != data.get(position).getData().getSubject() && !"".equals(data.get(position).getData().getSubject())) {
                        holder.tvDesc.setText(data.get(position).getData().getSubject());
                    } else {
                        holder.tvDesc.setText("");
                    }
                    if (null != data.get(position).getData().getPrice() && !"".equals(data.get(position).getData().getPrice())) {
                        holder.tvInnerPrice.setText(data.get(position).getData().getPrice());
                    } else {
                        holder.tvInnerPrice.setText("");
                    }
                    if (null != data.get(position).getData().getMarketprice() && !"".equals(data.get(position).getData().getMarketprice())) {
                        holder.tvMarketPrice.setText("原价：" + data.get(position).getData().getMarketprice() + "元");
                        holder.tvMarketPrice.setPaintFlags(Paint.STRIKE_THRU_TEXT_FLAG);
                    } else {
                        holder.tvMarketPrice.setText("原价：*元");
                        holder.tvMarketPrice.setPaintFlags(Paint.STRIKE_THRU_TEXT_FLAG);
                    }
                    break;
                case 4:
                    if (null != data.get(position).getData().getViewnum() && !"".equals(data.get(position).getData().getViewnum())) {
                        holder.tvView.setText(data.get(position).getData().getViewnum());
                    } else {
                        holder.tvView.setText("");
                    }
                    if (null != data.get(position).getData().getSubject() && !"".equals(data.get(position).getData().getSubject())) {
                        holder.tvDesc.setText(data.get(position).getData().getSubject());
                    } else {
                        holder.tvDesc.setText("");
                    }
                    holder.rb.setRating(data.get(position).getData().getStarlevel());
                    break;
            }
        }
        return convertView;
    }

    class ViewHolder{
        private ImageView ivImg;
        private ImageView ivLimit;
        private TextView tvName;
        private TextView tvTruePrice;
        private TextView tvCash;
        private TextView tvOriginPrice;
        private TextView tvRewardCoin;
        private TextView tvView;
        private TextView tvPrice;
        private TextView tvTime;
        private TextView tvDesc;
        private TextView tvInnerPrice;
        private TextView tvMarketPrice;
        private RatingBar rb;
    }

}
